import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt


picture_path = "../../res/111.jpg"


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # 创建垂直布局
        self.layout = QVBoxLayout()

        # 创建QLabel用于显示图片
        self.label = QLabel()
        # 加载图片
        global picture_path
        self.pixmap = QPixmap(picture_path)
        # 将图片设置给QLabel
        self.label.setPixmap(self.pixmap)
        # 使图片在QLabel中居中显示
        self.label.setAlignment(Qt.AlignCenter)

        # 将QLabel添加到布局中
        self.layout.addWidget(self.label)

        # 将布局设置给窗口
        self.setLayout(self.layout)
        # 设置窗口大小
        # self.resize(int(self.pixmap.width() / 8), int(self.pixmap.height() / 8))
        self.setGeometry(100, 100, self.pixmap.width(), self.pixmap.height())
        self.resize(self.pixmap.width(), self.pixmap.height())
        # 显示窗口
        self.show()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
